package org.fuxia.im.core.service.impl;

import java.nio.channels.SocketChannel;

import org.apache.log4j.Logger;
import org.fuxia.im.core.ECMessageType;
import org.fuxia.im.core.entries.ECPacket;
import org.fuxia.im.core.entries.ECSession;
import org.fuxia.im.core.server.ECSessionMananger;
import org.fuxia.im.core.socket.impl.ECNioServer;

public class FileTransferService extends AbstractService
{

	private static final Logger	logger	= Logger.getLogger(FileTransferService.class);

	public void service(ECPacket packet)
	{
		String toUser = packet.getTo();
		ECSession toUserSession = ECSessionMananger.getInstance().getUserSession(toUser);
		
		if(toUserSession==null) {
			ECPacket error = new ECPacket() ;
			error.setFrom(toUser);
			error.setTo(packet.getFrom()); 
			error.addParam("info", "对方不在线");
			error.setMessageType(ECMessageType.CANCEL_FILE_TRANSFER.name()) ;
			try
			{
				ECNioServer.sendPacket(error, ECSessionMananger.getInstance().getUserSession(packet.getFrom()).getChannel());
			}
			catch (Exception e)
			{
				logger.error("发送消息出错", e);
			}
			return ;
		}
		
		
		SocketChannel toChannel = toUserSession.getChannel();
		try
		{
			ECNioServer.sendPacket(packet, toChannel);
		}
		catch (Exception e)
		{
			logger.error("发送消息出错", e);
		}
	}
}
